package 牛客题霸;

import java.util.*;

public class LRU缓存设计 {

    public static int[] LRU (int[][] operators, int k) {
        List<Integer> result = new ArrayList<>();
        if(operators == null || operators.length <= 0) {
            return null;
        }
        List<Map<Integer,Integer>> list = new LinkedList<>();
        Map<Integer,Map<Integer, Integer>> map = new HashMap<>();
        for (int i = 0; i < operators.length; i++) {
            if(operators[i][0] == 1) {
                if(list.size() >= k) {
                    list.remove(0);
                }
                Map<Integer, Integer> subMap = new HashMap<>();
                subMap.put(operators[i][1],operators[i][2]);
                map.put(operators[i][1],subMap);
                list.add(subMap);
            } else {
                // 获取元素
                Integer value = -1;
                Integer key = operators[i][1];
                if (map.containsKey(key) && list.contains(map.get(key))) {
                    Map<Integer, Integer> maps = map.get(key);
                    value = maps.get(key);
                    list.remove(maps);
                    list.add(maps);
                }
                result.add(value);
            }

        }
        int[] a = new int[result.size()];
        for (int i = 0; i < result.size(); i++) {
            a[i] = result.get(i);
        }
        return a;
    }

    public static void main(String[] args) {
        int[][] a = new int[][]{{1,1,1},{1,2,2},{1,3,2},{2,1},{1,4,4},{2,2}};
        int[] lru = LRU(a, 3);
        System.out.println(Arrays.toString(lru));
    }

}
